# encoding=utf-8
class Market:
    """交易市场"""
    SH = ".XSHG"
    """上海A股"""
    SZ = ".XSHE"
    """深圳A股"""


class Side:
    """证券交易方向"""
    Buy = "BUY"
    """普通交易买,担保品买入"""
    Sell = "SELL"
    """普通交易卖,担保品卖出"""
    Bond_Buy = "BOND_BUY"
    Bond_Sell = "BOND_SELL"


class PriceType:
    """市价"""
    MARKET_ORDER = 'A'


class Bar:
    def __init__(self):
        self.symbol = None
        self.datetime = None
        self.open = None
        self.high = None
        self.low = None
        self.close = None
        self.volume = None
        self.balance = None


re_bond_entrust_dictionary_dic = {
    "BOND_BUY": "BOND_BUY",  # 债券买入
    "BOND_SELL": "BOND_SELL",  # 债券卖出
    "FINANCING": "FINANCING",  # 融资回购，正回购
    "MARGIN": "MARGIN"  # 融券回购，逆回购
}

future_direction_dic = {
    "OPEN": "1",
    "CLOSE": "2"
}
entrustdirection_dic = {
    "BUY": "1",
    "SELL": "2"
}


class StockFund:
    def __init__(self):
        self.avaliable_money = 0
        self.begin_balance = 0
        self.stock_asset = 0
        self.stock_cost = 0
        self.total_float_profit = 0
        self.total_profit = 0
        self.position_ratio = 0


order_status_fields = 'entrust_no,filled_amount,price,status,symbol,datetime,entrust_direction,futures_direction,order_id,amount,is_error,message,close_direction,withdraw_amount,investunit_id,instance_id'.split(
    ',')


class OrderStatus:
    def __init__(self):
        self.entrust_no = None
        self.filled_amount = None
        self.price = None
        self.status = None
        self.symbol = None
        self.datetime = None
        self.entrust_direction = None
        self.futures_direction = None
        self.order_id = None
        self.amount = None
        self.is_error = None
        self.message = None
        self.close_direction = None
        self.withdraw_amount = None
        self.investunit_id = None
        self.instance_id = None


trade_status_fields = 'order_id,trading_datetime,symbol,filled_amount,deal_price,entrust_direction,futures_direction,investunit_id,instance_id,close_direction,deal_no'.split(
    ',')


class TradeStatus:
    def __init__(self):
        self.order_id = None
        self.deal_time = None
        self.symbol = None
        self.amount = None
        self.price = None
        self.entrust_direction = None
        self.futures_direction = None
        self.investunit_id = None
        self.instance_id = None
        self.close_direction = None
        self.deal_no = None


class FunctionIds:
    """功能号"""
    # type = 1
    PLACE_ORDER = 10002
    CANCEL_ORDER = 10003

    # type = 3
    GET_SNAPSHOT = 30010
    SUBSCRIBE = 30011
    GET_TRADE_TIME = 30022
    GET_CONTRACT_INFO = 30023

    # type = 5
    SNAPSHOT = 40001
    ENTRUST_RESPONSE = 20001
    TRADE_RESPONSE = 20002

    QUERY_STOCK_FUND = 50001
    GET_MARGIN_FUND = 50302
    GET_AVAILABLE_MONEY = 50303
    GET_POSITION = 50100
    GET_OP_INSTANCE = 50105
    GET_EXT_ORDERS = 50106
    GET_EXT_TRADES = 50107
    GET_HIS_ORDERS = 50111

    # 1m subscribe
    M1_SUBSCRIBE = 50200

    GET_LATEST_HISTORY = 30020
    GET_PRICE_BY_DATE = 30021


function_name_id_dict = dict([(name, _id) for name, _id in FunctionIds.__dict__.items() if isinstance(_id, int)])

entrust_status_dict = {
    '0': '未报',
    '1': '未报',
    '2': '待报',
    '3': '正报',
    '4': '已报',
    '5': '废单',
    '6': '部成',
    '7': '已成',
    '8': '部撤',
    '9': '已撤',
    'a': '待撤',
    'b': '未审批',
    'c': '审批拒绝',
    'd': '未审批即撤销',
    'e': '撤单待审批',
    'A': '未撤(撤单委托)',
    'B': '待撤(撤单委托)'
}

entrust_direction_dict = {
    1: 'BUY',
    2: 'SELL'
}

FREQUENCY_DICT = {
    '1d': '1d',
    'daily': '1d',
    '1m': '1m',
    'minute': '1m',
    '5m': '5m',
    '15m': '15m',
    '30m': '30m',
    '60m': '60m',
}

kline_dictionary = {
    "1m": 1,
    "5m": 2,
    "15m": 3,
    "30m": 4,
    "60m": 5,
    "1d": 6,
    "w": 7,
    "mo": 8
}

fq_map = {'pre': 1, 'post': 2}
